package com.richapp.launcher.scene.conditions.sensor;

import com.richapp.launcher.database.system.dao.ConditionSensorModelDao;
import com.richapp.launcher.database.system.dao.SceneModelDao;
import com.richapp.launcher.database.system.mode.ConditionSensorModel;
import com.richapp.launcher.database.system.mode.DeviceModel;
import com.richapp.launcher.database.system.mode.SceneModel;
import com.richapp.launcher.scene.SceneServer;
import com.richapp.launcher.scene.abstracts.SceneAbstractSensorConditions;
import com.richapp.launcher.scene.interfaces.ISceneConditions;
import com.richapp.launcher.scene.interfaces.ISceneSensorConditions;
import com.richapp.net.util.HexTool;

public class SceneSensorCo2Conditions extends SceneAbstractSensorConditions {

	
	private int checkCO2Value;

	public SceneSensorCo2Conditions() {
		
	}

	@Override
	public int getConditionType() {
		return ISceneConditions.SCENE_CONDITION_TYPE_SENSOR;
	}

	@Override
	public int getConditionSubType() {
		return ISceneSensorConditions.SCENE_CONDITION_SUB_TYPE_SENSOR_CO2;
	}

	@Override
	public boolean checkCondition(String sceneName) {
		boolean checkFlag=false;
		DeviceModel device = getDevice();
		SceneModel sceneModel = new SceneModel();
		SceneModelDao sceneModelDao = new SceneModelDao(SceneServer.getInstance().getContext());
//		ConditionSensorModelDao conditionSensorModelDao=new ConditionSensorModelDao(SceneServer.getInstance().getContext());
		if (sceneName.equals("")){
			checkFlag = false;
		}
//		ConditionSensorModel conditionSensorModel=conditionSensorModelDao.getConditionSensorModel(sceneName);
		sceneModel=sceneModelDao.getSceneModelOnly(sceneName);
		if(device != null){
			switch (getConditionCheckModel()[0]) {
			case ISceneSensorConditions.SCENE_CONDITION_VALUE_CHECK_TYPE_GREATERTHAN:
				if(HexTool.HexToInt(device.getCarbonDioxide()) > checkCO2Value && "true".equals(sceneModel.getStatusCycleFlag())){
//					conditionSensorModel.setConditionCycleFlag("false");
//					conditionSensorModelDao.add(conditionSensorModel);
					checkFlag = true;
				}else if(HexTool.HexToInt(device.getCarbonDioxide()) <= checkCO2Value){
					sceneModel.setStatusCycleFlag("true");
					sceneModelDao.add(sceneModel);
				}
				break;

			case ISceneSensorConditions.SCENE_CONDITION_VALUE_CHECK_TYPE_EQUALS:
				if(HexTool.HexToInt(device.getCarbonDioxide()) == checkCO2Value && "true".equals(sceneModel.getStatusCycleFlag())){
//					conditionSensorModel.setConditionCycleFlag("false");
//					conditionSensorModelDao.add(conditionSensorModel);
					checkFlag = true;
				}else if (HexTool.HexToInt(device.getCarbonDioxide()) != checkCO2Value){
					sceneModel.setStatusCycleFlag("true");
					sceneModelDao.add(sceneModel);
				}
				break;
				
			case ISceneSensorConditions.SCENE_CONDITION_VALUE_CHECK_TYPE_LESSTHAN:
				if(HexTool.HexToInt(device.getCarbonDioxide()) < checkCO2Value && "true".equals(sceneModel.getStatusCycleFlag())){
//					conditionSensorModel.setConditionCycleFlag("false");
//					conditionSensorModelDao.add(conditionSensorModel);
					checkFlag = true;
				}else if (HexTool.HexToInt(device.getCarbonDioxide()) >= checkCO2Value){
					sceneModel.setStatusCycleFlag("true");
					sceneModelDao.add(sceneModel);
				}
				break;
				
			default:
				break;
			}
		}
		
		return checkFlag;
	}

	public int getCheckCO2Value() {
		return checkCO2Value;
	}

	public void setCheckCO2Value(int checkCO2Value) {
		this.checkCO2Value = checkCO2Value;
	}

}
